import 'package:hrf_app/common/extension/ex_visibility.dart';
import 'package:hrf_app/common/values/svgs.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';
import 'index.dart';

class MainMenuTabPage extends GetView<MainMenuTabController> {
  const MainMenuTabPage({super.key});

  // 主视图
  Widget _buildView() {
    return ListView(
      children: listView(),
    );
  }

//标题
  Widget _buildTitle(String title) {
    return Container(
        margin: const EdgeInsets.only(top: 12),
        child: TDDivider(
          text: title,
          textStyle: TextStyle(
              fontSize: 16, fontWeight: FontWeight.w600, color: Colors.black),
          alignment: TextAlignment.left,
        ));
  }

  ////菜单
  Widget _buildMenu(MenuItemModel menuItemModel) {
    return InkWell(
        onTap: () {
          controller.jumpToPage(menuItemModel);
        },
        child: Container(
            margin: const EdgeInsets.symmetric(vertical: 5, horizontal: 10),
            alignment: Alignment.center,
            // color: menuItemModel.color,
            decoration: BoxDecoration(
              borderRadius: const BorderRadius.all(Radius.circular(5)),
              boxShadow: const [BoxShadow(color: Colors.grey, blurRadius: 3)],
              color: menuItemModel.color,
            ),
            child: SizedBox(
              child: Text(
                menuItemModel.name,
              ),
            )));
  }

  List<Widget> listView() {
    List<Widget> list = [];
    List<MenuItemModel> menuList = controller.menuItemModelList;
    for (var element in menuList) {
      Widget textWidget = _buildTitle(element.name);
      list.add(textWidget);
      if (element.children!.isNotEmpty) {
        List<MenuItemModel> childrenList = element.children!;
        List<Widget> childrenlist = [];
        for (var vo in childrenList) {
          Widget textChildrenWidget = _buildMenu(vo);
          childrenlist.add(textChildrenWidget);
        }

        Widget gridView = GridView(

            ///子Item排列规则
            gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                //横轴元素个数
                crossAxisCount: 2,
                //纵轴间距
                mainAxisSpacing: 4.0,
                //横轴间距
                crossAxisSpacing: 12.0,
                //子组件宽高长度比例
                childAspectRatio: 2.0),
            physics: const NeverScrollableScrollPhysics(),
            shrinkWrap: true,

            ///GridView中使用的子Widegt
            children: childrenlist);
        list.add(gridView);
      }
    }

    return list;
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<MainMenuTabController>(
      init: MainMenuTabController(),
      id: "main_menu_tab",
      builder: (_) {
        return Scaffold(
          body: SafeArea(
            child: _buildView(),
          ),
        );
      },
    );
  }
}
